/*
 * Created on 10.11.2006 by leck
 *
 */
package de.devisnik.shifting.impl;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.graphics.Point;

import junit.framework.TestCase;

public class ShifterTest extends TestCase {

	private final class ShifterExtension extends Shifter {
		private ShifterExtension(IShiftable shiftable, int steps, int totalX,
				int totalY, List listeners) {
			super(shiftable, steps, totalX, totalY, listeners);
		}

		// @Override
		protected void schedule(Runnable runnable) {
			runnable.run();
		}
	}

	public void testShiftingSteps() {
		final int[] itsCounter = { 0 };
		IShiftable shiftable = new TestShiftable(0, 0) {
			// @Override
			public void setPosition(int x, int y) {
				super.setPosition(x, y);
				itsCounter[0]++;
			}
		};
		int steps = 10;
		Shifter shifter = new ShifterExtension(shiftable, steps, 0, 10, null);
		shifter.shift();
		assertEquals(10, itsCounter[0]);
	}

	public void testFinalPosition() {
		int startX = 10;
		int startY = 22;
		int steps = 10;
		int shiftX = 0;
		int shiftY = 10;
		IShiftable shiftable = new TestShiftable(startX, startY);
		Shifter shifter = new ShifterExtension(shiftable, steps, shiftX,
				shiftY, null);
		shifter.shift();
		assertEquals(new Point(startX, startY + shiftY), shiftable
				.getPosition());
	}

	public void testNotificationOnFinish() {
		List listeners = new ArrayList();
		final boolean[] notified = new boolean[1];
		IPuzzleCanvasListener listener = new IPuzzleCanvasListener() {

			public void doneShifting() {
				notified[0] = true;
			}
		};
		listeners.add(listener);
		ShifterExtension shifter = new ShifterExtension(
				new TestShiftable(0, 0), 1, 10, 10, listeners);
		shifter.shift();
		assertEquals(true, notified[0]);
	}

	public void testNotificationOnCanceled() {
		List listeners = new ArrayList();
		final boolean[] notified = new boolean[1];
		final int[] counter = new int[1];
		IPuzzleCanvasListener listener = new IPuzzleCanvasListener() {

			public void doneShifting() {
				notified[0] = true;
			}
		};
		listeners.add(listener);
		TestShiftable testShiftable = new TestShiftable(0, 0) {
			public void setPosition(int x, int y) {
				super.setPosition(x, y);
				counter[0]++;
			}
		};
		ShifterExtension shifter = new ShifterExtension(testShiftable, 1, 10,
				10, listeners);
		shifter.setCanceled();
		shifter.shift();
		assertEquals(true, notified[0]);
		assertEquals(0, counter[0]);
	}

}
